<?php
include('SwiftBase.class.php');
include('SwiftORM.class.php');
include('SwiftTable.class.php');

$config = parse_ini_file(realpath(dirname(__FILE__)).'/config.ini', true);
$ormObj = new SwiftORM($config);

function __autoload($className)
{
    global $ormObj;
    
    /**
     * convert class to table name
     */
    $pattern = "/(.)([A-Z])/"; 
    $replacement = "\\1 \\2";
    $tableName = strtolower(str_replace(' ', '_', preg_replace($pattern, $replacement, $className)));

    if ($ormObj->checkForTable($tableName))
    {
        /**
         * create the virtual class
         */
        eval('class '.$className.' extends SwiftTable
        {
            public function __construct()
            {
                global $ormObj;
                parent::__construct(__CLASS__, $ormObj->getConnection(), \'singular\', true);
            }
        }');
    }
}
